<?php

//异常日志写入数据库

namespace listen;
use app\admin\model\Admin\Log as LogModel;


class ExceptionLog
{
	
    public function handle($event){
		$content = request()->except(['s', '_pjax']);
		if ($content) {
            foreach ($content as $k => $v) {
                if (is_string($v) && strlen($v) > 200 || stripos($k, 'password') !== false) {
                    unset($content[$k]);
                }
            }
        }

        $ip = request()->ip();
        $data['ip'] = $ip;
        $data['city'] = \utils\IpAddress::getIpInfo($ip);

        $data['application_name'] = app('http')->getName();
        $data['username'] = '未知';
		$data['url'] = request()->url(true);
		$data['useragent'] = request()->server('HTTP_USER_AGENT');
		$data['content'] = json_encode($content);
		$data['errmsg'] = $event;
		$data['create_time'] = date('Y-m-d H:i:s');
		$data['type'] = 3;
        $data['status'] = 1;

        $loginfo = LogModel::create($data);
        if($loginfo->id){
            LogModel::update(['sort_id'=>$loginfo->id,'id'=>$loginfo->id]);
        }
    }
	
}